﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AuthDemo.Web;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.ServiceModel.DomainServices.Client;

namespace AuthDemo
{
    public partial class MainPage : UserControl
    {
        AuthenticationService auth = WebContext.Current.Authentication;
        MyDomainContext ctx = new MyDomainContext();
        public MainPage()
        {
            InitializeComponent();

            this.DataContext = WebContext.Current;
            this.DataArea.DataContext = ctx;

            Btn_Save.DataContext = WebContext.Current;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (!auth.User.Identity.IsAuthenticated)
            {
                Txt_Result.Text = "Please login first!";
                Txt_Result.Foreground = new SolidColorBrush(Colors.Red);
                return;
            }

            var invop = ctx.GetHelloMessage();

            invop.Completed += (s, e1) =>
                {
                    if (invop.HasError)
                    {
                        MessageBox.Show(invop.Error.Message);
                        invop.MarkErrorAsHandled();
                    }
                    else
                    {
                        Txt_Result.Text = "Result from service call: " + invop.Value.ToString();
                    }
                };
        }

        private void Btn_Login_Click(object sender, RoutedEventArgs e)
        {
            LoginOperation lo = auth.Login(Txt_UserName.Text, Txt_Pwd.Password);
            lo.Completed += (s, args) =>
                {
                    if (lo.HasError)
                    {
                        MessageBox.Show(lo.Error.Message);
                        lo.MarkErrorAsHandled();
                    }
                    else
                    {
                        if (lo.LoginSuccess)
                        {
                            Txt_Result.Foreground = new SolidColorBrush(Colors.Green);
                            Txt_Result.Text = "Login Succeeded";
                        }
                        else
                        {
                            Txt_Result.Foreground = new SolidColorBrush(Colors.Red);
                            Txt_Result.Text = "Login Failed";
                        }
                    }
                };

        }

        private void Btn_Logout_Click(object sender, RoutedEventArgs e)
        {
            auth.Logout(lo =>
                {
                    if (lo.HasError)
                    {
                        MessageBox.Show(lo.Error.Message);
                        lo.MarkErrorAsHandled();
                    }
                    else
                    {
                        Txt_Result.Foreground = new SolidColorBrush(Colors.Green);
                        Txt_Result.Text = "Logout Success";

                    }
                }, null);
        }

        private void Btn_SaveInfo_Click(object sender, RoutedEventArgs e)
        {
            auth.SaveUser(so =>
                {
                    Txt_Result.Text = "User data Save";

                }, null);
        }

        private void Btn_LoadData_Click(object sender, RoutedEventArgs e)
        {
            var query = ctx.GetWidgetsQuery().Where(x => x.Owner == auth.User.Identity.Name);

            ctx.Load(query, LoadBehavior.RefreshCurrent, (io) =>
                {
                    if (io.HasError)
                    {
                        MessageBox.Show(io.Error.Message);
                        io.MarkErrorAsHandled();
                    }
                    else
                    {
                        this.DG_Widget.ItemsSource = io.Entities;
                    }
                }, null);
        }

        private void Btn_Save_Click(object sender, RoutedEventArgs e)
        {
            ctx.SubmitChanges((io) =>
            {
                if (io.HasError)
                {
                    MessageBox.Show(io.Error.Message);
                    io.MarkErrorAsHandled();
                }
            }, null);
        }

        private void widgetDomainDataSource_LoadedData(object sender, System.Windows.Controls.LoadedDataEventArgs e)
        {
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }
    }
}
